package cn.meye11.crm.service;

import cn.meye11.crm.common.RestResponse;
import cn.meye11.crm.common.StringUtils;
import cn.meye11.crm.domain.entity.CostEntity;
import cn.meye11.crm.domain.model.Cost;
import cn.meye11.crm.domain.query.CostQuery;
import cn.meye11.crm.mapper.CostMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Cost)表服务实现类
 * 默认CRUD操作
 *
 * @author makejava
 * @since 2024-01-03 14:41:34
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class CostService {

    private final CostMapper costMapper;
    private final ObjectMapper objectMapper;

    private final ApplicationEventPublisher applicationEventPublisher;
    // 代码写到此区域


    /************************************* 请在上方编写新代码 **************************************/
    // 此区域为自动生成的通用代码 尽量不直接修改
    @Transactional(rollbackFor = Exception.class)
    public Long save(Cost model) {
        if (StringUtils.isNotBlank(model.getOperators())) {
            model.setCreatedBy(model.getOperators());
            model.setModifiedBy(model.getOperators());
        } else {
            model.setCreatedBy("Genkai");
            model.setModifiedBy("Genkai");
        }

        if (StringUtils.isBlank(model.getCreatedBy())) {
            throw new RuntimeException("创建人不能为空");
        }
        if (model.getId() != null) {
            throw new RuntimeException("主键id不可自定义");
        }
        if (model.getVersion() != null) {
            throw new RuntimeException("数据版本号不可自定义");
        }
        if (model.getTime() != null) {
            throw new RuntimeException("数据创建时间不可自定义");
        }
        if (model.getLast() != null) {
            throw new RuntimeException("数据修改时间不可自定义");
        }

        CostEntity entity = new CostEntity(model);
        costMapper.insertSelective(entity);

        return entity.getId();
    }

    public void remove(Cost model) {
        model.setModifiedBy(StringUtils.isBlank(model.getOperators()) ? "zhz" : model.getOperators());
        if (model.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }

        if (StringUtils.isBlank(model.getModifiedBy())) {
            throw new RuntimeException("修改人不能为空");
        }

        CostEntity entity = costMapper.selectByPrimaryKey(model.getId());
        if (entity == null) {
            throw new RuntimeException("ID:" + model.getId() + "不存在");
        }
        costMapper.deleteByPrimaryKey(model.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(Cost model) {
        model.setModifiedBy(StringUtils.isBlank(model.getOperators()) ? "zhz" : model.getOperators());
        if (model.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }

        if (StringUtils.isBlank(model.getModifiedBy())) {
            throw new RuntimeException("修改人不能为空");
        }

        CostEntity entity = costMapper.selectByPrimaryKey(model.getId());
        if (entity == null) {
            throw new RuntimeException("ID:" + model.getId() + "不存在");
        }

        CostEntity save = new CostEntity(model);
        save.setLast(LocalDateTime.now());
        save.setTime(entity.getTime());
        costMapper.updateByPrimaryKey(save);
    }

    public Cost get(Long id) {
        CostEntity entity = costMapper.selectByPrimaryKey(id);
        return entity == null ? null : new Cost(entity);
    }

    public Long count(CostQuery query) {
        return costMapper.count(query);
    }

    public RestResponse<List<Cost>> list(CostQuery query) {
        // 单次查不得大于32767条数据，否则需要分页
        query.setLimit((int) Short.MAX_VALUE);
        return page(query);
    }

    public RestResponse<List<Cost>> page(CostQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<CostEntity> list = costMapper.list(query);
        PageInfo<CostEntity> count = new PageInfo<>(list);
        List<Cost> collect = list.stream().map(Cost::new).collect(Collectors.toList());
        return RestResponse.success(count.getTotal(), collect);
    }
}
